<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends Controller {

	function Ajax ()
	{
		parent::Controller();	
		$this->load->library('flexigrid');
	}
	
  //to display city details....
  
   
  function city_list($id=0)
  {
    $this->load->model('cities_model');
    $valid_fields = array('city_id','city_name');
    
    $this->flexigrid->validate_post('city_name','asc',$valid_fields);

    $records = $this->cities_model->get_cities($id);
    
    $this->output->set_header($this->config->item('json_header'));
    
    $i =1;
    foreach ($records['records']->result() as $row)
    {
      $record_items[] = array($row->city_id,
      $i,
      '<span style=\'color:#ff4400\'><a href="'.base_url().'cities/city_edit/'.$row->city_id.'">'.addslashes($row->city_name).'</a></span>',
       
          );
      $i++;   
            
    }
    if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    
  }
  
    
  
  //to display area details....
   
  function areas_list($id=0)
  {
    $this->load->model('area_model');
    $valid_fields = array('area_id','area_name');
    
    $this->flexigrid->validate_post('area_name','asc',$valid_fields);

    $records = $this->area_model->get_areas($id);
    
    $this->output->set_header($this->config->item('json_header'));
    
    $i =1;
    foreach ($records['records']->result() as $row)
    {
      $record_items[] = array($row->area_id,
      $i,
      '<span style=\'color:#ff4400\'><a href="'.base_url().'areas/area_edit/'.$row->area_id.'">'.addslashes($row->area_name).'</a></span>',
      '<span >'.addslashes($row->area_pincode).'</span>' 
          );
      $i++;   
            
    }
    if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    
  }
  
  //to display poi details in grid....
  
  function poi_details()
  {
    $this->load->model('poi_model');
    $valid_fields = array('poi_id','poi_desc');
    
    $this->flexigrid->validate_post('task_desc','asc',$valid_fields);

    $records = $this->poi_model->details_poi();
    
    $this->output->set_header($this->config->item('json_header'));
    
    $i =1;
    foreach ($records['records']->result() as $row)
    {
      $record_items[] = array($row->poi_id,
      $i,
      '<span style=\'color:#ff4400\'><a href="'.base_url().'poi/poi_edit/'.$row->poi_id.'">'.addslashes($row->poi_desc).'</a></span>' 
          );
      $i++;   
    }
     if($records['record_count'])
      {
          $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
      else
      {
        $record_items[] = array('No');
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
  }
  
  //to display task details in grid....
  
  function task_list()
  {
    $this->load->model('task_model');
    $valid_fields = array('task_id','task_desc');
    
    $this->flexigrid->validate_post('task_desc','asc',$valid_fields);

    $records = $this->task_model->display_task();
    
    $this->output->set_header($this->config->item('json_header'));
    
    $i =1;
    foreach ($records['records']->result() as $row)
    {
      $record_items[] = array($row->task_id,
      $i,
      '<span style=\'color:#ff4400\'><a href="'.base_url().'task/task_edit/'.$row->task_id.'">'.addslashes($row->task_desc).'</a></span>' 
          );
      $i++;   
            
    }
     if($records['record_count'])
      {
          $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
      else
      {
        $record_items[] = array('No');
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
  }
  
  //to display state details....
   
	function states_list()
	{
		$this->load->model('states_model');
		$valid_fields = array('state_id','state_name');
		
		$this->flexigrid->validate_post('state_name','asc',$valid_fields);

		$records = $this->states_model->get_states();
		
		$this->output->set_header($this->config->item('json_header'));
		
		$i =1;
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->state_id,
			$i,
			'<span style=\'color:#ff4400\'><a href="'.base_url().'states/state_edit/'.$row->state_id.'">'.addslashes($row->state_name).'</a></span>' 
					);
			$i++;		
						
		}
		if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
	}
	
  //to display user information in user control...
  
  function user_list()
  {
    $this->load->model('user_model');
    $valid_fields = array('user_id','user_name');
    
    $this->flexigrid->validate_post('user_name','asc',$valid_fields);

    $records = $this->user_model->get_users();
    
    $this->output->set_header($this->config->item('json_header'));
    
    $i =1;
    foreach ($records['records']->result() as $row)
    {
      $record_items[] = array($row->user_id,
      $i,
      '<span style=\'color:#ff4400\'><a href="'.base_url().'user/user_edit/'.$row->user_id.'">'.addslashes($row->user_name).'</a></span>',
       '<span style=\'color:#ff4400\'>'.addslashes($row->user_phone).'</span>',
       '<span style=\'color:#ff4400\'>'.addslashes($row->user_email).'</span>',
       '<span style=\'color:#ff4400\'>'.addslashes($row->user_number).'</span>',
          );
      $i++;   
            
    }
    if($records['record_count'])
      {
          $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
      else
      {
        $record_items[] = array('No');
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
      }
  }
  
	//used to display qc list 
	function qc_list()
	{
		$this->load->model('caf_model');
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('batch_mobilenum','caf_customername');
		
		$this->flexigrid->validate_post('caf_id','asc',$valid_fields);

		$records = $this->caf_model->get_qclist();
		
		$this->output->set_header($this->config->item('json_header'));
		
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		 $i=1;
		foreach ($records['records']->result() as $row)
		{
			if($row->caf_id)
			{
				$check = $this->caf_model->check_qcdone($row->caf_id);
				$count = $check->result_array();
   //     print_r($row);exit;
				if($count[0]['count(qc_id)'])
				{
					$record_items[] = array($row->caf_id,
					$i,
					'<span style=\'color:#ff4400\'><a href="'.base_url().'qc_caf/qc_data/'.$row->sl_num.'">'.addslashes($row->batch_mobilenum).'</a></span>',
					'<span style=\'color:#ff4400\'><a href="'.base_url().'qc_caf/qc_data/'.$row->sl_num.'">'.addslashes($row->caf_customername).'</a></span>','<span style=\'color:#FF0086\'>Rejected!</span>'
								);
				$i++;					
				}
				else
				{
			
					$record_items[] = array($row->caf_id,
					$i,
					'<span style=\'color:#ff4400\'><a href="'.base_url().'qc_caf/qc_data/'.$row->sl_num.'">'.addslashes($row->batch_mobilenum).'</a></span>',
					'<span style=\'color:#ff4400\'><a href="'.base_url().'qc_caf/qc_data/'.$row->sl_num.'">'.addslashes($row->caf_customername).'</a></span>','<span style=\'color:#81BE7A\'>Pending</span>'
					);
					$i++;
				}
			
			}
		}

		if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
	}
	
	function approve_list()
	{
		$this->load->model('caf_model');
		$valid_fields = array('batch_mobilenum','caf_customername');
		
		$this->flexigrid->validate_post('caf_id','asc',$valid_fields);

		$records = $this->caf_model->toget_approve();
		
		$this->output->set_header($this->config->item('json_header'));
		
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		 $i=1;
		foreach ($records['records']->result() as $row)
		{
			if($row->caf_id)
			{
				$check = $this->caf_model->check_already_approved($row->caf_id);
				$count = $check->result_array();
				if(!$count[0]['count(softcopy_id)'])
				{
					$record_items[] = array(
  					$row->caf_id,
  					$i,
  					'<span style=\'color:#ff4400\'><a href="'.base_url().'data_approval/scan_qc/'.$row->caf_id.'">'.addslashes($row->batch_mobilenum).'</a></span>',
  					'<span style=\'color:#ff4400\'>'.addslashes($row->caf_customername).'</span>',
					);
					$i++;
								
				}		
			
			}
		}
		if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
	}
	
	function phonenum_list()
	{
		$this->load->model('caf_model');
		
		$valid_fields = array('batch_mobilenum');
		
		$this->flexigrid->validate_post('sl_num','asc',$valid_fields);
		
		//to get new phone numbers...
		$records = $this->caf_model->get_new_phonenum();
		
		$this->output->set_header($this->config->item('json_header'));
		
		/*

		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		 $i=1;
		foreach ($records['records']->result() as $row)
		{
					list($ay,$am,$ad) = explode('-',$row->activation_date); 
					list($ry,$rm,$rd) = explode('-',$row->received_date); 
					
          if($row->caf_entry == 0)
          {
            $entry_status = "<span style='color:red;'>Not Done!</span>";
            $phone_num = '<span ><a href="'.base_url().'caf/data_entry/'.$row->sl_num.'">'.addslashes($row->batch_mobilenum).'</a></span>';
          }
          else
          {
            $entry_status = "<span style='color:red;'>Done</span>";
            $phone_num = $row->batch_mobilenum;
          }
          
          if($row->qc_check == 0)
          {
            $qc_status = "<span style='color:red;'>Not Done!</span>";
          }
          else
          {
            $qc_status = "<span style='color:red;'>Done</span>";
          }
          
					$record_items[] = array($row->batch_id,
					$row->sl_num,
					'<span >'.addslashes($row->batch_number).'</span>',
					$phone_num,'<span >'.addslashes($ad.'-'.$am.'-'.$ay).'</span>',
					'<span >'.addslashes($rd.'-'.$rm.'-'.$ry).'</span>',
					$entry_status,$qc_status,
					
					);
					$i++;
								
						
			
		
		}
    
		if($records['record_count'])
    {
        $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }
    else
    {
      $record_items[] = array('No');
      $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
    }

	}
	
}
?>
